package com.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.controller.TalentsController;
import com.group.entity.ProjectOutside;
import com.group.entity.Talents;
import com.group.entity.User;
import com.group.mapper.TalentsMapper;
import com.group.service.TalentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group.vo.DataVO;
import com.group.vo.TalentsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 朋心合力
 * @since 2021-08-09
 */
@Service
public class TalentsServiceImpl extends ServiceImpl<TalentsMapper, Talents> implements TalentsService {

    public Page<ProjectOutside> getPages(Integer currentPage, Integer pageSize, List list) {
        Page page = new Page();
        int size = list.size();

        if (pageSize > size) {
            pageSize = size;
        }

        // 求出最大页数，防止currentPage越界
        int maxPage = size % pageSize == 0 ? size / pageSize : size / pageSize + 1;

        if (currentPage > maxPage) {
            currentPage = maxPage;
        }

        // 当前页第一条数据的下标
        int curIdx = currentPage > 1 ? (currentPage - 1) * pageSize : 0;

        List pageList = new ArrayList();

        // 将当前页的数据放进pageList
        for (int i = 0; i < pageSize && curIdx + i < size; i++) {
            pageList.add(list.get(curIdx + i));
        }

        page.setCurrent(currentPage).setSize(pageSize).setTotal(list.size()).setRecords(pageList);
        return page;
    }

    @Autowired
    TalentsMapper talentsMapper;

    @Override
    public List<Talents> findTalents(String tag1, String tag2) {
        return talentsMapper.findTalents(tag1, tag2);
    }

    @Override
    public DataVO<TalentsVO> findData(Integer page, Integer limit, String talentTag) {
        DataVO dataVO = new DataVO();
        List<Talents> talentsList = null;
        dataVO.setCode(0);
        dataVO.setMsg("");
        IPage<Talents> iPage = new Page<>(page, limit);
        QueryWrapper wrapper = new QueryWrapper();
        if (talentTag.equals("全部技术领域") || talentTag.equals("")) {
            IPage<Talents> talentsIPage = talentsMapper.selectPage(iPage, null);
            dataVO.setCount(talentsIPage.getTotal());
            talentsList = talentsIPage.getRecords();
        } else {
            wrapper.like("talent_tag", talentTag);
            IPage<Talents> talentIPage = talentsMapper.selectPage(iPage, wrapper);
            dataVO.setCount(talentIPage.getTotal());
            talentsList = talentIPage.getRecords();
        }
        List<TalentsVO> talentsVOList = new ArrayList<>();
        for (Talents talents : talentsList) {
            TalentsVO talentsVO = new TalentsVO();
            BeanUtils.copyProperties(talents, talentsVO);
            talentsVOList.add(talentsVO);
        }
        dataVO.setData(talentsVOList);
        return dataVO;
    }

    @Override
    public DataVO<TalentsVO> findData2(Integer page, Integer limit) {
        DataVO dataVO = new DataVO();
        List<Talents> talentsList = null;
        dataVO.setCode(0);
        dataVO.setMsg("");
        IPage<Talents> iPage = new Page<>(page, limit);
        IPage<Talents> talentsIPage = talentsMapper.selectPage(iPage, null);
        dataVO.setCount(talentsIPage.getTotal());
        talentsList = talentsIPage.getRecords();
        List<TalentsVO> talentsVOList = new ArrayList<>();
        for (Talents talents : talentsList) {
            TalentsVO talentsVO = new TalentsVO();
            BeanUtils.copyProperties(talents, talentsVO);
            talentsVOList.add(talentsVO);
        }
        dataVO.setData(talentsVOList);
        return dataVO;
    }
}
